﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using libtcodWrapper;
using Console = libtcodWrapper.Console;

namespace HDHack
{
    class DropInterface
    {
        Console displayConsole;
        public bool IsActive = false;
        Dungeon theDungeon;
        Player p;
        List<Item> DropableItems;

        public DropInterface(Player ip, Console dCon, Dungeon d)
        {
            p = ip;
            displayConsole = dCon;
            theDungeon = d;
        }

        //not all items will be dropable... but currently they are.
        private List<Item> GetDropableItems(List<Item> il)
        {
            List<Item> eList = new List<Item>();
            foreach (Item i in il)
            {
                if(!i.IsEquiped)
                    eList.Add(i);
            }
            return eList;
        }

        int ConvertItemID(int charVal)
        {
            int retVal = charVal - 97;
            return retVal;
        }

        public void ProcessChoice(KeyPress k)
        {
            int choiceIndex = ConvertItemID(k.Character);
            if (Convert.ToInt16(k.Character) > 96 && choiceIndex < DropableItems.Count)
            {
                DropableItems[choiceIndex].OnMap = false;
                p.DropItem(DropableItems[choiceIndex]);
                DropableItems[choiceIndex].x = p.x;
                DropableItems[choiceIndex].y = p.y;
                theDungeon.Levels[theDungeon.CurrentLevelIndex].tiles[p.x, p.y].items.Add(DropableItems[choiceIndex]);
                DropableItems[choiceIndex].OnMap = true;
                DropableItems.Clear();
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
            else if (k.KeyCode == KeyCode.TCODK_ESCAPE)
            {
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
        }

        public void DisplayDropableInventory()
        {
            displayConsole.Clear();
            int lastline = 1;
            int itemASCIIIndex = 97;
            DropableItems = GetDropableItems(p.Inventory);


            if (DropableItems.Count > 0)
            {
                lastline = displayConsole.PrintLineRect("Drop which item?", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                lastline++;
                displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                lastline++;


                foreach (Item i in DropableItems)
                {
                    displayConsole.PrintLine("(" + Convert.ToChar(itemASCIIIndex) + ") " + i.name, 1, lastline, LineAlignment.Left);
                    itemASCIIIndex++;
                    lastline++;

                }
                IsActive = true;

            }
            else
            {
                displayConsole.PrintLine("Nothing to drop!", 1, lastline, LineAlignment.Left);
                theDungeon.ComplexCommand = false;
                IsActive = false;

            }
        }

    }
}
